/**
 * Created by dragon on 2015/12/3.
 */
require.config({
    baseUrl:PUBLIC + "/Admin/js/lib"
});
define(function(require){
    var $ = require('jquery');
    window.$ = window.jQuery = $;
    require('jquery.form');
    var c=require('../common');
    var dialog = require('bDialog/dialog');
    require('tips/msgbox');
    require('select');
    require('fileupload/fupload');

    $(function(){
        c.init();

        if ($('.fileupload').length) {
            $('.fileupload').each(function(){
                var uploadObj = $(this).closest('.upload-area');

                var path = uploadObj.find('.upload_path').val();//存储文件夹
                var field = uploadObj.find('.input_field').val();//表单的name
                var maxNum = uploadObj.find('.maxNum').val();
                var role = uploadObj.find('.role').val();
                var uid = uploadObj.find('.uid').val();

                var icoh = 0;
                if(maxNum > 1){
                    icoh = 1;
                }

                $(this).fupload({path: path, input: field, max: maxNum,imgType:'big',role:role,uid:uid,icoh:icoh});
            });
        }

        var foldCategory = function(){
            $('.tree img').click(function(){
                var next=$(this).closest('p').next();
                if(next.css('display')=='block'){
                    next.slideUp();
                    $(this).attr('src',PUBLIC+'/Admin/images/on.png');
                }else{
                    next.slideDown();
                    $(this).attr('src',PUBLIC+'/Admin/images/off.png');
                }
            });
        }

        foldCategory();


        /**
         * 添加子分类
         * rel=[cid,stable,limit,level]
         * @parma integer cid 分类ID
         * @param string stable 所操作的数据表
         * @parma integer limit 字数限制
         * @return boolean false
         */

        $('.so_main').delegate('[data-add]','click',function(){
            var t=$(this);
            var rel=$(this).data('add').split(',');
            var cid=rel[0],stable=rel[1],limit=rel[2],level=rel[3];
            if(typeof cid === "undefined" || typeof stable === "undefined") {
                return false;
            }
            var gurl=URL+"/addtreecategory?"+'&cid='+cid+'&stable='+stable+'&limit='+limit+'&level='+level;
            dialog({id:'dl',title:'添加栏目',url:gurl,width:480,end:function(){
                $('select').selectbox();
            },ok:function(){
                upTreeCategory(1,cid,t);
                return false;
            },cancel:function(){}});

            return false;
        });
        $('.so_main').delegate('[data-edit]','click',function(){
            var t=$(this);
            var rel=$(this).data('edit').split(',');
            var cid=rel[0],stable=rel[1],level=rel[2];
            if(typeof cid === "undefined" || typeof stable === "undefined") {
                return false;
            }
            var gurl=URL+"/update_nav?"+'&cid='+cid+'&stable='+stable+'&level='+level;
            dialog({id:'dl',title:'编辑栏目',url:gurl,width:480,end:function(){
                $('select').selectbox();
            },ok:function(){
                upTreeCategory(2,cid,t);
                return false;
            },cancel:function(){}});
        });

        /**
         * 添加,修改分类操作
         * @return void
         */
        var upTreeCategory = function(act,i,t){
            var id = $.trim($('#cid').val());
            var level = parseInt($("#nodelist").find('option:selected').attr('level'))+1;
            var pid = $.trim($('#nodelist').val());
            var action = $.trim($('#action').val());
            var title = $.trim($('#title').val());
            var keywords = $.trim($('#keywords').val());
            var des = $.trim($('#des').val());
            var menu = $.trim($('#menu').val());


            // 判断是否输入内容
            if(menu==''){
                c.error('栏目名称不能为空！',1500);
                return false;
            }else if(action==''){
                c.error('栏目动作不能为空！',1500);
                return false;
            }
            var data={cid:id,menu:menu,pid:pid,level:level,title:title,action:action,keywords:keywords,des:des};

            // 提交添加
            $.post(URL+"/doAddTreeCategory",data, function(msg) {
                if(msg.status == 1) {
                    c.success(msg.info,1500);
                    dialog.list['dl'].remove();
                    $('.tree .noinfo').remove();
                    if(act==1){
                        if(i==0){
                            var ul=$('.tree > ul');
                            if(ul.length==0){
                                ul=$('<ul></ul>')
                                $('.tree').append(ul);
                            }
                            ul.append(msg.data);
                            ul.show();
                        }else{
                            var ul=$(t).closest('p').next();
                            if(ul.length==0){
                                ul=$('<ul></ul>')
                                $(t).closest('p').after(ul);
                            }
                            ul.append(msg.data);
                            ul.show();
                        }
                    }else{
                        $(t).closest('p').find('.name').html(msg.data);
                    }
                } else {
                    c.error(msg.info,1500);
                    return false;
                }
            }, 'json');
        };
        $('.so_main').delegate('[data-del]','click',function(){
            var li=$(this).closest('li');
            var cid=li.data('id');
            var stable =$(this).data('del');

            if(typeof cid === "undefined") {
                return false;
            }
            c.confirm('是否删除该栏目？',function(){
                $.post(URL+"/rmTreeCategory", {cid:cid, stable:stable}, function(msg) {
                    if(msg.status == 1) {
                        c.success(msg.info,1500,function(){
                            li.fadeOut(function(){
                                $(this).remove();
                            });
                            dialog.list['cdl'].remove();
                        });
                    } else {
                        c.error(msg.info,1500);
                    }
                }, 'json');
            });
            return false;
        });
        $('.so_main').delegate('a[data-move]','click',function(){
            var rel=$(this).data('move').split(',');
            var gurl=URL+'/moveTreeCategory';
            c.moveTreeCategory(gurl,rel[0],rel[1],rel[2]);
            return false;
        });

        //更新或添加地区
        var updateArea = function(id,t){
            var n = t.attr('class');
            var type = (n!='edit_category') ? 1 : 2;
            var title = (n != 'edit_category') ? '添加地区' : '修改地区';
            var d = dialog({title:title,width:520,url:URL+'/edit/id/'+id+'/type/'+type,end:function(){
                $('#pid_sel').selectbox({w:273});
            },ok:function(){
                var name = $.trim($('input[name="name"]').val());
                var short = $.trim($('input[name="short"]').val());
                var area_code = $.trim($('input[name="area_code"]').val());
                var sort = $.trim($('input[name="sort"]').val());
                var id = $.trim($('input[name="id"]').val());

                // 判断是否输入内容
                if(empty(name)){
                    c.error('地区名称不能为空!',2000);
                    $('input[name="name"]').focus();
                    return false;
                }

                var forms = {};
                forms['name'] = name;
                forms['short'] = short;
                forms['area_code'] = area_code;
                forms['sort'] = sort;
                forms['id'] = id;

                $.post(URL+'/edit',forms,function(msg){
                    if(msg.status == 1) {
                        c.success(msg.info,1000);
                        if(type==1 || empty(type)) {
                            if (msg.data) {
                                if (empty(pid)) {
                                    //主节点
                                    $('.tree > ul').append(msg.data);
                                } else {
                                    //子节点
                                    var ul = t.closest('p').next('ul');
                                    if (ul.length == 0) {
                                        ul = $('<ul></ul>');
                                        t.closest('p').after(ul);
                                    }
                                    ul.append(msg.data);
                                    ul.show();
                                }
                            }
                        }else{
                            t.closest('p').find('.title').html(name);
                        }
                        d.remove();
                    } else {
                        c.error(msg.info,2000);
                        d.remove();
                        return false;
                    }
                });
                return false;
            },cancel:function(){},okVal:'保存',cancelVal:'取消'});
            return false;
        };

        //添加或更新洲/国家/州/城市
        var updateCountry = function(id,t){
            var level = t.data('level');
            var url = URL + '/edit?level=' + level;
            var dialogTitle = '';
            var op = '';
            if(t.hasClass('add_category')){
                op = '添加';
                dialogTitle = '洲';
            } else {
                if(t.hasClass('edit_category')){
                    op = '编辑';
                    url += '&id=' + id;
                    switch(parseInt(level)){
                        case 1:
                            dialogTitle += '洲';
                            break;

                        case 2:
                            dialogTitle += '国家';
                            break;

                        case 3:
                            dialogTitle += '州';
                            break;

                        case 4:
                            dialogTitle += '城市';
                            break;
                    }
                } else {
                    op = '添加';
                    url += '&pid=' + id;
                    switch(parseInt(level)){
                        case 1:
                            dialogTitle = '国家';
                            break;

                        case 2:
                            dialogTitle = '州';
                            break;

                        case 3:
                            dialogTitle = '城市';
                            break;
                    }
                }
            }
            var d = dialog({title:op+dialogTitle,width:530,url:url,end:function(){
                $('#pid_sel').selectbox();
            },ok:function(){
                var body = this.c('body');
                var name = $.trim(body.find('input[name="name"]').val());
                var short = $.trim(body.find('input[name="short"]').val());
                var status = $.trim(body.find('input[name="status"]:checked').val());
                var sort = $.trim(body.find('input[name="sort"]').val());
                var pid = $.trim(body.find('input[name="pid"]').val());
                var id = $.trim(body.find('input[name="id"]').val());
                var level = parseInt($.trim(body.find('input[name="level"]').val()));

                var support = [];
                body.find('input[name="support[]"]').each(function(){
                    if(this.checked){
                        support.push(this.value);
                    }
                });

                // 判断是否输入内容
                if(empty(name)){
                    c.error(dialogTitle + '名称不能为空!',2000);
                    return false;
                }
                if(getLength(name)>50){
                    c.error(dialogTitle + '名称不能超过50个字!',2000);
                    return false;
                }

                if(empty(short)){
                    c.error(dialogTitle + '简称不能为空!',2000);
                    return false;
                }
                if(getLength(short)>20){
                    c.error(dialogTitle + '简称不能超过20个字!',2000);
                    return false;
                }

                var forms = {};
                forms['name'] = name;
                forms['short'] = short;
                forms['support'] = support;
                forms['status'] = status;
                forms['sort'] = sort;
                forms['level'] = level;
                forms['id'] = id;
                forms['pid'] = pid;

                $.post(URL+'/update',forms,function(msg){
                    if(msg.status == 1) {
                        c.success(msg.info,1000);
                        if(empty(id)) {
                            if (msg.data) {
                                if (parseInt(pid) == 0) {
                                    location.reload();
                                } else {
                                    //子节点
                                    var ul = t.closest('p').next('ul');

                                    var html = '<li><p>';
                                    html += '<img id="img_'+msg.data+'" src="'+PUBLIC+'/Admin/images/off.png" />';
                                    html += '<span class="title">'+name+'</span>';
                                    html += '<span class="c3">';
                                    var separator = '';
                                    var countryRelationHtml = '';
                                    var countryRecommendHtml = '';
                                    if(level<4){
                                        var addSubText = '';
                                        var addCountryHtml = '';

                                        switch(level){
                                            case 1:
                                                addSubText = '添加国家';
                                                break;

                                            case 2:
                                                addSubText = '添加州（省）';
                                                addCountryHtml = '&nbsp;&nbsp;-&nbsp;&nbsp;<a class="add_sub_category" data-id="'+msg.data+'" data-mark="country" data-level="'+(level+1)+'" href="javascript:;">添加城市</a>';
                                                countryRelationHtml = '&nbsp;&nbsp;-&nbsp;&nbsp;<a class="relation_set" data-id="'+msg.data+'" data-mark="country" data-level="'+(level+1)+'" href="country?id='+msg.data+'">设置国家相关</a>';
                                                countryRecommendHtml = '&nbsp;&nbsp;-&nbsp;&nbsp;<a class="recommend_set" data-id="'+msg.data+'" data-mark="country" data-level="'+(level+1)+'" data-recommend="2" href="javascript:;">推荐</a>';
                                                break;

                                            case 3:
                                                addSubText = '添加城市';
                                                break;
                                        }
                                        html += separator+'<a class="add_sub_category" data-id="'+msg.data+'" data-mark="country" data-level="'+level+'" href="javascript:;">'+addSubText+'</a>';
                                        separator = '&nbsp;&nbsp;-&nbsp;&nbsp;';
                                        html += addCountryHtml;
                                    }
                                    html += separator+'<a class="edit_category" data-id="'+msg.data+'" data-mark="country" data-level="'+level+'" href="javascript:void(0);">修改</a>';
                                    separator = '&nbsp;&nbsp;-&nbsp;&nbsp;';
                                    html += separator+'<a class="status_category" data-status="'+status+'" data-id="'+msg.data+'" data-mark="country" data-level="'+level+'" href="javascript:void(0);">禁用</a>';
                                    html += separator+'<a class="del_category" data-id="'+msg.data+'" data-mark="country" data-level="'+level+'" href="javascript:void(0);">删除</a>';
                                    html += countryRecommendHtml;
                                    html += countryRelationHtml;
                                    html += '</span>';
                                    html += '<span class="c2 center">';
                                    html += '<a class="ico_top mr5" data-id="'+msg.data+'" data-mark="country" href="javascript:;"></a>';
                                    html += '<a class="ico_btm" data-id="'+msg.data+'" data-mark="country" href="javascript:;"></a>';
                                    html += '</span>';
                                    html += '</p></li>';

                                    if (ul.length == 0) {
                                        ul = $('<ul></ul>');
                                        t.closest('p').after(ul);
                                        ul.append(html);
                                        ul.show();
                                    } else {
                                        //t.closest('p').siblings('ul').find('li:last').after(html);
                                        t.closest('p').siblings('ul').append(html);
                                    }
                                    foldCategory();
                                    cateFunc();
                                }
                            }
                        }else{
                            t.closest('p').find('.title').html(name);
                        }
                        d.remove();
                    } else {
                        c.error(msg.info,2000);
                        d.remove();
                        return false;
                    }
                });
                return false;
            },cancel:function(){},okVal:'保存',cancelVal:'取消'});
            return false;
        }

        //添加或更新项目分类
        var updateItemCategory = function(id,t){
            var type = parseInt(t.data('type'));
            var url = URL + '/edit?type=' + type;
            var dialogTitle = '移民项目分类';
            var op = '添加';
            var pid = 0;
            if(t.hasClass('edit_category')){
                op = '编辑';
                url += '&id=' + id;
            } else {
                pid = id;
                id = 0;
                url += '&pid=' + pid;
            }
            if(type == 2){
                dialogTitle = '房产项目分类';
            }

            var d = dialog({title:op+dialogTitle,width:526,url:url,end:function(){
                var body = this.c('body');
                body.find('select').selectbox();

                body.find('input[name="has_spec"]').off().on('click',function(){
                    if(this.value==1){
                        body.find('.spec').removeClass('hidden');
                    } else {
                        body.find('.spec').removeClass('hidden').addClass('hidden');
                    }
                });
            },ok:function(){
                var body = this.c('body');
                var name = $.trim(body.find('input[name="name"]').val());
                var status = $.trim(body.find('input[name="status"]:checked').val());
                var sort = $.trim(body.find('input[name="sort"]').val());
                var layer = $.trim(body.find('input[name="layer"]').val());
                var pid = $.trim(body.find('input[name="pid"]').val());
                var formtype = $.trim(body.find('select[name="formtype"]').val());
                var is_main = $.trim(body.find('input[name="is_main"]:checked').val());
                if(type == 2){
                    var has_spec = $.trim(body.find('input[name="has_spec"]:checked').val());
                    var area = $.trim(body.find('input[name="area"]:checked').val());
                    var price = $.trim(body.find('input[name="price"]:checked').val());
                    var has_assort = $.trim(body.find('input[name="has_assort"]:checked').val());
                } else {
                    var is_need_pay = $.trim(body.find('input[name="is_need_pay"]:checked').val());
                }

                // 判断是否输入内容
                if(empty(name)){
                    c.error(dialogTitle + '名称不能为空!',2000);
                    return false;
                }
                if(getLength(name)>50){
                    c.error(dialogTitle + '名称不能超过50个字!',2000);
                    return false;
                }

                var forms = {};
                forms['name'] = name;
                forms['status'] = status;
                forms['is_main'] = is_main;
                forms['formtype'] = formtype;
                forms['sort'] = sort;
                forms['id'] = id;
                forms['pid'] = pid;
                forms['type'] = type;
                forms['layer'] = layer;
                if(type == 2){
                    forms['has_spec'] = $.trim(body.find('input[name="has_spec"]:checked').val());
                    forms['area'] = $.trim(body.find('input[name="area"]:checked').val());
                    forms['price'] = $.trim(body.find('input[name="price"]:checked').val());
                    forms['has_assort'] = $.trim(body.find('input[name="has_assort"]:checked').val());
                } else {
                    forms['is_need_pay'] = is_need_pay;
                }

                $.post(URL+'/update',forms,function(msg){
                    if(msg.status == 1) {
                        c.success(msg.info,1000);
                        if(empty(id)) {
                            if (msg.data) {
                                var html = '<li><p>';
                                html += '<img id="img_'+msg.data+'" src="'+PUBLIC+'/Admin/images/off.png" />';
                                html += '<span class="title">'+name+'</span>';
                                html += '<span class="c3">';
                                var separator = '';
                                var hierarchy = parseInt(t.data('hierarchy'));
                                if(parseInt(pid) == 0 || (hierarchy < 3 && layer > 1)){
                                    var addSubText = '添加子类';
                                    html += separator+'<a class="add_sub_category" data-id="'+msg.data+'" data-hierarchy="'+ (hierarchy + 1) +'" data-mark="item_category" data-type="'+type+'" href="javascript:;">'+addSubText+'</a>';
                                    separator = '&nbsp;&nbsp;-&nbsp;&nbsp;';
                                }
                                html += separator+'<a class="edit_category" data-id="'+msg.data+'" data-hierarchy="'+ (hierarchy + 1) +'" data-mark="item_category" data-type="'+type+'" href="javascript:void(0);">修改</a>';
                                separator = '&nbsp;&nbsp;-&nbsp;&nbsp;';
                                html += separator+'<a class="status_category" data-status="'+status+'" data-hierarchy="'+ (hierarchy + 1) +'" data-id="'+msg.data+'" data-mark="item_category" data-type="'+type+'" href="javascript:void(0);">禁用</a>';
                                if((type == 1) && (pid==1)) html += separator + '<a class="relation_country" data-mark="item_category" data-hierarchy="'+ (hierarchy + 1) +'" data-hierarchy="1" data-id="'+msg.data+'" data-type="'+type+'" href="javascript:void(0);">设置关联国家</a>';
                                html += separator+'<a class="del_category" data-id="'+msg.data+'" data-hierarchy="'+ (hierarchy + 1) +'" data-mark="item_category" data-type="'+type+'" href="javascript:void(0);">删除</a>';
                                html += '</span>';
                                html += '<span class="c2 center">';
                                html += '<a class="ico_top mr5" data-id="'+msg.data+'" data-mark="item_category" data-type="'+type+'" href="javascript:;"></a>';
                                html += '<a class="ico_btm" data-id="'+msg.data+'" data-mark="item_category" data-type="'+type+'" href="javascript:;"></a>';
                                html += '</span>';
                                html += '</p></li>';

                                if (parseInt(pid) == 0) {
                                    $('.tree ul').append(html);
                                } else {
                                    //子节点
                                    var ul = t.closest('p').next('ul');

                                    if (ul.length == 0) {
                                        ul = $('<ul></ul>');
                                        t.closest('p').after(ul);
                                        ul.append(html);
                                        ul.show();
                                    } else {
                                        t.closest('p').siblings('ul').find('li:last').after(html);
                                    }
                                }
                                foldCategory();
                                cateFunc();
                            }
                        }else{
                            t.closest('p').find('.title').html(name);
                        }
                        d.remove();
                    } else {
                        c.error(msg.info,2000);
                        d.remove();
                        return false;
                    }
                });
                return false;
            },cancel:function(){},okVal:'保存',cancelVal:'取消'});
            return false;
        }

        var delCategory = function(id,t){
            dialog({title:'温馨提示',height:100,icon:'warning',content:'是否确定删除？',ok:function(){
                $.get(URL+'/del?id='+id,'',function(data){
                    if(data.status==1){
                        c.success(data.info,1000);
                        var li = t.closest('li');
                        if(li.siblings().length){
                            li.remove();
                        }else{
                            t.closest('ul').remove();
                        }
                    }else if(data.status == 2){
                        c.error(data.info,1,2000);
                        return false;
                    }else {
                        c.error(data.info,2000);
                        return false;
                    }
                },'json');
            },
                cancel:function(){},
                okVal:'确定',cancelVal:'取消'});
        };

        var moveCategory = function(id,type){
            var forms = {};
            forms['id'] = id;
            forms['type'] = type;

            $.post(URL+'/sort',forms,function(data){
                if(data.status==1){
                    c.success(data.info,1000,function(){
                        location.reload();
                    });

                } else {
                    c.error(data.info);
                }
            },'json');
        }

        var cateFunc = function(){
            //编辑分类、添加子分类、添加分类
            $('.add_category,.add_sub_category,.edit_category').off().on('click',function(){
                var t = $(this);
                var id = t.data('id');
                if(empty(id)){
                    id = 0;
                }
                var mark = t.data('mark');

                switch(mark){
                    case 'area':
                        updateArea(id,t);
                        break;

                    case 'country':
                        updateCountry(id,t);
                        break;

                    case 'item_category':
                        updateItemCategory(id,t);
                        break;
                }
            });

            //删除分类
            $('.del_category').off().on('click',function(){
                var t = $(this);
                var id = t.data('id');

                if(empty(id)){
                    c.error('请选择操作项！',2000);
                    return false
                }

                //var mark = t.data('mark');

                delCategory(id,t);
            });

            //排序——升序
            $('.tree').delegate('.ico_top','click',function(){
                var t = $(this);
                var id = t.data('id');

                if(empty(id)){
                    c.error('请选择操作项！',2000);
                    return false
                }

                moveCategory(id,1);
            });

            //排序——降序
            $('.tree').delegate('.ico_btm','click',function(){
                var t = $(this);
                var id = t.data('id');

                if(empty(id)){
                    c.error('请选择操作项！',2000);
                    return false
                }

                moveCategory(id,2);
            });

            //启用与禁用
            $('.status_category').off().on('click',function(){
                var t = $(this);
                var id = t.data('id');
                var status = parseInt(t.data('status'));

                if(empty(id)){
                    c.error('请选择操作项！',2000);
                    return false
                }

                var statusFunc = function(id,status){
                    $.post(URL+'/setStatus',{'id':id,'status':status},function(data){
                        if(data.status==1){
                            c.success(data.info,1000,function(){
                                location.reload();
                            })
                        } else {
                            c.error(data.info,2000);
                        }
                    },'json');
                };

                if(status == 1){
                    if(c.confirm('确定要禁用？',function(){
                        statusFunc(id,2);
                    }));
                } else {
                    statusFunc(id,1);
                }
            });

            //设置关联国家
            $('.relation_country').off().on('click',function(){
                var t = $(this);
                var id = t.data('id');

                if(empty(id)){
                    c.error('请选择操作项！',2000);
                    return false
                }

                var url = URL + '/country?id=' + id;
                var d = dialog({title:'设置关联国家',width:620,url:url,end:function(){
                    var body=this.c('body');
                    body.append('<div class="dlresult"><span class="fat">您选择的是：</span><div class="sel"></div></div>');
                    var btn=body.find('.btn-primary'),
                        sel=body.find('.dlresult .sel'),
                        selected=body.find('.selected').html();

                    body.delegate('[name="item[]"]','change',function(){
                        var input=$(this);
                        if(input.is(':checked')){
                            createli({id:input.val(),name:input.attr('title')});
                        }else{
                            sel.find('[data-code="'+input.val()+'"]').remove();
                        }
                    });
                    sel.delegate("a", "click", function() {
                        var id=$(this).data('code');
                        body.find('input[data-sid="'+id+'"]').prop("checked",false);
                        $(this).remove();
                    });
                    var createli=function(j){
                        var _item = $("<a />").attr("data-code", j.id).appendTo(sel);
                        $("<span />").html(j.name).appendTo(_item);
                        $('<i />').appendTo(_item);
                    }
                    if(selected){
                        eval('var a='+selected+';');
                        $.each(a,function(i,j){
                            createli(j);
                            body.find('input[data-sid="'+ j.id+'"]').prop("checked",true);
                        });
                    }
                },ok:function(){
                    var body=this.c('body');

                    var countryIdArr = [];
                    body.find('input[name="item[]"]').each(function(){
                        if(this.checked){
                            countryIdArr.push(this.value);
                        }
                    });

                    if(empty(countryIdArr)){
                        c.error('请选择国家',2000);
                        return false;
                    }

                    $.post(URL+'/country',{'id':id,'country':countryIdArr},function(data){
                        if(data.status == 1){
                            c.success(data.info,1000,function(){ d.close(); })
                        } else {
                            c.error(data.info,2000);
                        }
                    },'json');
                    return false;
                },cancel:function(){}});
            });

            //推荐或取消推荐国家
            $('.recommend_set').off().on('click',function(){
                var t = $(this);
                var id = t.data('id');
                var recommend = parseInt(t.data('recommend'));

                dialog({title:'温馨提示',height:100,icon:'warning',content:'是否确定'+ t.html()+'此国家？',ok:function(){
                    $.get(URL+'/setRecommend?id='+id+'&recommend='+recommend,'',function(data){
                        if(data.status==1){
                            c.success(data.info,1000);
                            t.data('recommend',recommend==1?2:1);
                            t.html(recommend==1?'推荐':'取消推荐');
                        } else {
                            c.error(data.info,2000);
                            return false;
                        }
                    },'json');
                },
                    cancel:function(){},
                    okVal:'确定',cancelVal:'取消'});
            });
        }

        cateFunc();

        $('#detail_country_form').ajaxForm({
            beforeSubmit:  checkForm,  // pre-submit callback
            success:       complete,  // post-submit callback
            dataType: 'json'
        });

        function checkForm(){
            return true;
        }

        function complete(data){
            if (data.status==1){
                c.success(data.info,1000);
                setTimeout(function(){
                    location.href= URL+ '/index';
                },1500);
            }else{
                c.error(data.info,2000);
                return false;
            }
        }
    });
});